package codegen.client.mapper;

import java.util.Date;
import java.util.List;

import uk.co.q3c.deplan.client.ui.component.Q3CheckBox;
import uk.co.q3c.deplan.client.ui.component.Q3DateBox;
import uk.co.q3c.deplan.client.ui.component.ReferenceListWidget;
import uk.co.q3c.deplan.client.ui.component.ReferenceWidget;
import uk.co.q3c.deplan.client.ui.component.SublistWidget;
import uk.co.q3c.deplan.client.ui.component.TextBoxDouble;
import uk.co.q3c.deplan.client.ui.component.TextBoxInteger;
import uk.co.q3c.deplan.client.ui.component.TextBoxLong;
import uk.co.q3c.deplan.client.ui.component.TextBoxString;

import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.objectify.Key;

public enum WidgetDef {

	CHECKBOX(Q3CheckBox.class, boolean.class, true),
	TEXTBOXINTEGER(TextBoxInteger.class, int.class, false),
	TEXTBOXLONG(TextBoxLong.class, long.class, false),
	TEXTBOXDOUBLE(TextBoxDouble.class, double.class, false),
	DATEBOX(Q3DateBox.class, Date.class, false),
	REFERENCEWIDGET(ReferenceWidget.class, Key.class, true),
	LISTBOX(ListBox.class, List.class, false),
	REFERENCELISTWIDGET(ReferenceListWidget.class, List.class, true),
	NULL(null, null, false),
	SUBLISTWIDGET(SublistWidget.class, List.class, true),
	TEXTBOXSTRING(TextBoxString.class, String.class, false);
	private Class<?> valueType;
	private String getterMethod;
	private String setterMethod;
	private Class<? extends Widget> clazz;
	private boolean selfLabel;

	private WidgetDef(Class<? extends Widget> clazz, Class<?> valueType, boolean selfLabel) {
		this.valueType = valueType;
		this.getterMethod = "getDataValue";
		this.setterMethod = "setDataValue";
		this.clazz = clazz;
		this.selfLabel = selfLabel;
	}

	public Class<?> valueType() {
		return valueType;
	}

	public String getValueMethod() {
		return getterMethod + "()";
	}

	public String setValueMethod(String paramName) {
		return "void " + setterMethod + "(" + valueType.getSimpleName() + " " + paramName + ")";
	}

	public Class<? extends Widget> clazz() {
		return clazz;
	}

	public String callSetValueMethod(String called, String paramName) {
		return called + "." + setterMethod + "(" + paramName + ")";
	}

	public String construct(String widgetName) {
		return widgetName + " = new " + clazz.getSimpleName() + "()";
	}

	public boolean isSelfLabel() {
		return selfLabel;
	}

	public void setSelfLabel(boolean selfLabel) {
		this.selfLabel = selfLabel;
	}

}
